#include "stdafx.h"
#ifdef WIN32
#include <io.h>
#else
#include <unistd.h> 
#endif

#include "TString.h"

typedef unsigned long  u_long;
#define CONST_PATH_SEP                    '/' 

/* Architecture of this host */
#define PACKAGE_MACHINE "x86_64"
/* SVN release of this package */
#define PACKAGE_RELEASE "r328"
/* Define to the version of this package. */
#define PACKAGE_VERSION "1.3"

char mProgram[64];


void Usage() {

  printf("%s %s v.%s - (C) 2017-12 atdm.liao\n\n"
	 "Usage:\n"
	 "  %s [-h] | [ -f file ] | [ string ]\n",
	 mProgram, PACKAGE_MACHINE, PACKAGE_VERSION,mProgram);
}

int GetFileString(const char * name,TString& dest)
{
  FILE * fp = fopen(name,"rb");
  if( fp == NULL )	  return -1;
  
  fseek(fp,0,SEEK_END);
  int length = ftell(fp);
  char * tmp = dest.GetBuffer(length);
  fseek(fp,0,SEEK_SET);
  fread(tmp,1,length,fp);
  fclose(fp);
  dest.ReleaseBuffer();
	return length;
}
void StrCopy(const char * src,char * dest)
{
   int length = strlen(src);
   memset(dest,0,length+1);
   char ch;
   int index=0;
   for(int i=0;i<length;i++){
	   ch = src[i];
	   if( ch=='\\' ) { dest[index++]=src[i+1];i++;}
	   else dest[index++]=ch;
   }
}
static void JsonString(TString & dest, const char *str)
{
static const char *json_hex_chars = "0123456789abcdef";
  int pos = 0;
  unsigned char c;
  while (str[pos]) {
    c = str[pos++];
    switch(c) {
	case '"':
	case '/':
	case '\\':
		dest.Append('\\');
		dest.Append(c);
		break;
	case '\b':
		dest.Append('\\');
		dest.Append('b');
		break;
	case '\f':
		dest.Append('\\');
		dest.Append('f');
		break;
	case '\t':
		dest.Append('\\');
		dest.Append('t');
		break;
	case '\n':
		dest.Append('\\');
		dest.Append('n');
		break;
	case '\r':
		dest.Append('\\');
		dest.Append('r');
		break;
    default:
      if(c < ' ') {
			dest.Append('\\');
			dest.Append('u');
		  dest.Append(json_hex_chars[c >> 4]);
		  dest.Append(json_hex_chars[c & 0xf]);
	  } else dest.Append(c);
    }
  }
}

int fileinput=0;
int main(int argc,char * argv[])
{
      char * cp;
	  int i;
      if ((cp = strrchr(argv[0], CONST_PATH_SEP)) != NULL)
		strcpy(mProgram ,cp + 1 );
	else strcpy(mProgram ,argv[0]);
      if ((cp = strrchr(mProgram, '.')) != NULL)	cp[0]=0;
	  if( argc<2 ) {
		  Usage();
		  return -1;
	  }
	  if( strcmp(argv[1],"-h")==0 ) {
		  Usage();
		  return 0;
	  }
	  if( strcmp(argv[1],"--h")==0 ) {
		  Usage();
		  return 0;
	  }

	  int optIndex =1;
	  if( strcmp(argv[1],"-f")==0 ) {
		  fileinput=1;
		  optIndex ++;
	  }

	  if( optIndex >= argc ) { 
		  printf("%s: missing input.\n",mProgram);
		  Usage(); return -1; 
	  }
	  TString input;
	  if( fileinput ){ 
		 // TString s;
		  GetFileString(argv[optIndex],input);
	  } else {
		  for( i=2;i<argc;i++){
			  input.Append(' ');
			  input.Append(argv[i]);
		  }
	  }
	  if( input.GetLength()<2 ){
		  printf("%s: missing input.\n",mProgram);
		  Usage();
		  return -1;
	  }
	  TString out;
	  JsonString(out,(LPCTSTR)input);
	  printf("%s",(LPCTSTR)out);
	  return 0;
}
